﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WINBASE.Controls.GridSearchBar
{
    public partial class SingleRangeBar : UserControl
    {
        public BindingList<KeyValuePair<string, string>> Column1, Column2;

        public SingleRangeBar()
        {
            InitializeComponent();
        }

        #region Event

        private void tspControl_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem ctrl = ((ToolStripItem)e.ClickedItem);
            switch (ctrl.Name)
            {
                case "btnClear":
                    txtFrom.Text = "";
                    txtTo.Text = "";
                    txtFrom.Focus();
                    break;
                default:
                    break;
            }
        }

        void dataGrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Search();
        }

        private void txtSearch1_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void txtSearch2_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void cboColumn1_SelectedChanged(object sender, EventArgs e)
        {
            Search();
        }

        #endregion

        #region Method

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            InitializeUI();
        }

        public void InitializeUI()
        {
            this.Dock = DockStyle.Top;
        }

        public void InitColumn()
        {
            Column1 = new BindingList<KeyValuePair<string, string>>();

            foreach (DataGridViewColumn dgvCol in this.DataGrid.Columns)
            {
                if (dgvCol.Visible && dgvCol.HeaderText != "")
                {
                    string key = dgvCol.DataPropertyName;
                    string value = dgvCol.HeaderText;

                    Column1.Add(new KeyValuePair<string, string>(key, value));    
                }
            }

            cboColumn1.DataSource = Column1;
            cboColumn1.ValueMember = "Key";
            cboColumn1.DisplayMember = "Value";

            this.SetComboIndex();
        }

        public void SetComboIndex()
        {
            base.Refresh();
            if (this.DataGrid.SortedColumn != null && this.DataGrid.SortedColumn.Visible == true)
                this.SelectedIndex = this.DataGrid.SortedColumn.DisplayIndex;
            else this.SelectedIndex = this.DataGrid.Columns.Count > 0 ? 0 : -1;
        }

        public void Search()
        {
            if (this.DataGrid != null && this.DataGrid.DataSource != null)
            {
                BindingSource bsFilter = new BindingSource();
                bsFilter.DataSource = ((DataTable)this.DataGrid.DataSource);
                bsFilter.Filter = QueryFilter;
                //((DataTable)this.DataGrid.DataSource).DefaultView.RowFilter = QueryFilter;
            }
        }

        public void InitializeData()
        {
            if (this.DataGrid != null)
            {
                this.DataGrid.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGrid_ColumnHeaderMouseClick);
                InitColumn();
            }
        }

        #endregion

        #region Property

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WLCDataGridView DataGrid { get; set; }

        public string QueryFilter
        {
            get
            {
                string strQf = "";
                if (cboColumn1.SelectedIndex > -1 && txtFrom.Text.Trim() != "")
                {
                    if (txtFrom.Text.Trim() != "" & txtTo.Text.Trim() != "")
                    {
                        strQf += (strQf.Length <= 0 ? "" : " AND ");
                        strQf += string.Format("[{0}] Between '{1}' in '{2}'", 
                            cboColumn1.Text,
                            txtFrom.Text.Trim(),
                            txtTo.Text.Trim());
                    }
                    else
                    {
                        string searchValue;
                        if (txtFrom.Text.Trim() != "") searchValue = txtFrom.Text.Trim();
                        else searchValue = txtTo.Text.Trim();

                        strQf += (strQf.Length <= 0 ? "" : " AND ");
                        strQf += string.Format("CONVERT([{0}], System.String) like '{1}%'",
                                                cboColumn1.Text.Trim(), searchValue);
                        //strQf += '[' + cboColumn1.Text + ']' + " LIKE('" + searchValue + "%')";
                    }
                }
                return strQf;
            }
        }

        private int _SelectedIndex = -1;
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set { _SelectedIndex = value; }
        }

        #endregion

        
    }
}